package de.jmda.mview.proj;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ListSelectionModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProjectModelSelectionDialog
    extends JDialog
{
  private class ListModel extends AbstractListModel
  {
    private static final long serialVersionUID = 1L;

    List<ProjectModel> projects = new ArrayList<ProjectModel>();
  	
    private ListModel(Set<ProjectModel> projects)
  	{
    	this.projects.addAll(projects);
  	}

		@Override
    public Object getElementAt(int index)
    {
	    return projects.get(index).getName();
    }

		@Override
    public int getSize()
    {
	    return projects.size();
    }
		
		public ProjectModel getProject(int index)
		{
			if (index < 0)
			{
				return null;
			}
			
			if (index > (projects.size() - 1))
			{
				return null;
			}
			
			return projects.get(index);
		}
  }

  private static final long serialVersionUID = 1L;

  private final JPanel contentPanel = new JPanel();
  
	private JList list = new JList();
	
	private ListModel listModel;
	
	private ProjectModel selectedProjectModel;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		try
		{
			ProjectModelSelectionDialog dialog = new ProjectModelSelectionDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	private ProjectModelSelectionDialog()
	{
		setTitle("select project");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{0, 0, 0};
		gbl_contentPanel.rowHeights = new int[]{0, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			GridBagConstraints gbc_list = new GridBagConstraints();
			gbc_list.weighty = 1.0;
			gbc_list.weightx = 1.0;
			gbc_list.fill = GridBagConstraints.BOTH;
			gbc_list.gridx = 1;
			gbc_list.gridy = 1;
			contentPanel.add(list, gbc_list);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						selectedProjectModel = listModel.getProject(list.getSelectedIndex());
						setVisible(false);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						selectedProjectModel = null;
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	public ProjectModelSelectionDialog(Set<ProjectModel> projects)
	{
		this();
		listModel = new ListModel(projects);
		list.setModel(listModel);
	}

	public ProjectModel getSelectedProjectModel()
	{
		return selectedProjectModel;
	}
}